using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;

using Gizmox.WebGUI.Forms;

namespace TVC.Modules.Act
{
    /// <summary>
    /// Helper class for manipulating with ScheduleBox events
    /// </summary>
    public class Events
    {
        #region Local Variable
        public MainForm MainForm { get; set; }
        private DateTime? _planMinDate;
        private DateTime? _planMaxDate;
        private DateTime? _actMaxDate;
        private DateTime? _actMinDate;
        //System.Globalization.CultureInfo mobjCustomCulture = null;
        //System.Globalization.CultureInfo mobjSavedCulture = null;
        public DateTime? actMinDate
        {
            get
            {
                return _actMinDate;
            }
            set
            {
                if (_actMinDate == value)
                    return;
                _actMinDate = value;
            }
        }
        public DateTime? planMaxDate
        {
            get
            {
                return _planMaxDate;
            }
            set
            {
                if (_planMaxDate == value)
                    return;
                _planMaxDate = value;
            }
        }
        public DateTime? actMaxDate
        {
            get
            {
                return _actMaxDate;
            }
            set
            {
                if (_actMaxDate == value)
                    return;
                _actMaxDate = value;
            }
        }
        public DateTime? planMinDate
        {
            get
            {
                return _planMinDate;
            }
            set
            {
                if (_planMinDate == value)
                    return;
                _planMinDate = value;
            }
        }
        #endregion

        public Events(MainForm form)
        {
            string sErr = "";
            MainForm = form;

            
        }
        /// <summary>
        /// Wraps creation of a ScheduleBoxEvent
        /// </summary>
        public ScheduleBoxEvent Create(DateTime start, DateTime end, string subject, string tag)
        {
            ScheduleBoxEvent objEvent = new ScheduleBoxEvent() { Start = start, End = end, Subject = subject, Tag = tag };

            return objEvent;
        }

        /// <summary>
        /// Opens an editing form and adds new event to demoScheduleBox if user confirmed.
        /// Recreates context menu by cloning given eventmenu and attaches the handler for click
        /// for each menu item.
        /// </summary>
        /// <param name="demoScheduleBox">The schedule box.</param>
        /// <param name="eventmenu">The eventmenu with OR without clicked event.</param>
        /// <param name="handler">The handler to response to right click.</param>
        /// <param name="blnCopy">New event created as a copy of clicked eventmenu.Event object.</param>
        public void ProcessAddPlan(string emp, ScheduleBox demoScheduleBox, bool blnCopy, ScheduleBoxEvent objEvent, ContextMenu menu)
        {
            ScheduleBoxEvent newEvent = null;
            if (blnCopy && objEvent != null)
            {
                // Copy data from given event
                newEvent = Create(objEvent.Start, objEvent.End, objEvent.Subject,
                    objEvent.Tag != null ? objEvent.Tag.ToString() : "");
            }
            else
            {
                // Create absolutely new event
                newEvent = new ScheduleBoxEvent();
                newEvent.Start = DateTime.Now;
                newEvent.End = newEvent.Start.AddHours(1);
            }

            if (newEvent != null)
            {
                

            }
        }

        private void LoadPlans(string emp, ScheduleBox demoScheduleBox, ContextMenu menu)
        {
            DateTime from = new DateTime(demoScheduleBox.FirstDate.Year, demoScheduleBox.FirstDate.Month, demoScheduleBox.FirstDate.Day);
            DateTime to = from;
            switch (demoScheduleBox.View)
            {
                case ScheduleBoxView.Day:
                    to = from.AddDays(1);
                    break;
                case ScheduleBoxView.Week:
                    to = from.AddDays(6);
                    break;
                case ScheduleBoxView.Month:
                    from = new DateTime(demoScheduleBox.FirstDate.Year, demoScheduleBox.FirstDate.Month, 1);
                    to = from.AddMonths(1);
                    break;
                case ScheduleBoxView.FullWeek:
                    to = from.AddMonths(8);
                    break;
            }
            to = new DateTime(to.Year, to.Month, to.Day);
            LoadPlans(emp, demoScheduleBox, from, to, menu);
        }
        private void LoadActiveties(string emp, ScheduleBox demoScheduleBox, ContextMenu menu)
        {
            DateTime from = new DateTime(demoScheduleBox.FirstDate.Year, demoScheduleBox.FirstDate.Month, demoScheduleBox.FirstDate.Day);
            DateTime to = from;
            switch (demoScheduleBox.View)
            {
                case ScheduleBoxView.Day:
                    to = from.AddDays(1);
                    break;
                case ScheduleBoxView.Week:
                    to = from.AddDays(6);
                    break;
                case ScheduleBoxView.Month:
                    from = new DateTime(demoScheduleBox.FirstDate.Year, demoScheduleBox.FirstDate.Month, 1);
                    to = from.AddMonths(1);
                    break;
                case ScheduleBoxView.FullWeek:
                    to = from.AddMonths(8);
                    break;
            }
            to = new DateTime(to.Year, to.Month, to.Day);
            LoadActiveties(emp, demoScheduleBox, from, to, menu);
        }
        public void ProcessAddAct(string emp, ScheduleBox demoScheduleBox, bool blnCopy, ScheduleBoxEvent objEvent, string strAction, NavigationTabs navigationTabs, ContextMenu menu)
        {
            ScheduleBoxEvent newEvent = null;
            if (blnCopy && objEvent != null)
            {
                // Copy data from given event
                newEvent = Create(objEvent.Start, objEvent.End, objEvent.Subject,
                    objEvent.Tag != null ? objEvent.Tag.ToString() : "");
            }
            else
            {
                // Create absolutely new event
                newEvent = new ScheduleBoxEvent();
                newEvent.Start = DateTime.Now;
                newEvent.End = newEvent.Start.AddHours(1);
            }

            if (newEvent != null)
            {
                // Opening editing form and attach a context menu
                Forms.frmMain eventForm = new Forms.frmMain(MainForm) { EmpID = emp, TabsMains = navigationTabs };

                if (strAction == "AddEvent")
                {
                    eventForm.FormType = "OAC";
                }
                else
                    eventForm.FormType = strAction;
                eventForm.StrAction = "C";

                DateTime minday = demoScheduleBox.FirstDate;
                //if (_actMinDate != null && _actMinDate.Value > minday)
                //    minday = _actMinDate.Value;

                DateTime maxDay = minday.AddDays(8);
                if (demoScheduleBox.View == ScheduleBoxView.Week)
                    maxDay = demoScheduleBox.FirstDate.AddDays(6);
                else if (demoScheduleBox.View == ScheduleBoxView.Month)
                {
                    maxDay = demoScheduleBox.FirstDate.AddDays(31);
                }
                //if (_actMaxDate != null && _actMaxDate.Value < maxDay)
                //    maxDay = _actMaxDate.Value;
                //if (MainForm.IsLockAct(maxDay))
                //{
                //    MessageBox.Show(clsConfigurarion.GetErrMsg("E0021", MainForm.Language));
                //    return;
                //}
                eventForm.SetTimeRange(minday, maxDay);

                eventForm.FormClosed += (form, eventargs) =>
                {
                    if (((Form)form).DialogResult == DialogResult.OK)
                    {
                        LoadActiveties(emp, demoScheduleBox, menu);
                    }
                };
                eventForm.ShowDialog();

            }
        }
        /// <summary>
        /// Opens the editing form
        /// </summary>
        public void ProcessOpenEvent(string emp,
            ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {
            

        }

        /// <summary>
        /// Deletes the objEvent if user confirmed.
        /// </summary>
        public void ProcessDeleteEvent(ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {
            
        }



        public void ProcessDeletePlan(
            ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {
            
        }
        public void ProcessDeleteAct(ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {
           
        }
        public void ProcessOpenPlan(string emp, ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {
            

        }
        private void FormatDate()
        {
            //if (mobjCustomCulture == null)
            //{
            //    mobjCustomCulture = System.Globalization.CultureInfo.CreateSpecificCulture(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            //    string[] myDateTimePatterns = new string[] { "ddd dd/MM/yy", "ddd dd/MM/yyyy" };
            //    mobjCustomCulture.DateTimeFormat.SetAllDateTimePatterns(myDateTimePatterns, 'd');
            //}
            //if (mobjSavedCulture == null)
            //    mobjSavedCulture = System.Threading.Thread.CurrentThread.CurrentCulture;

            //System.Threading.Thread.CurrentThread.CurrentCulture = mobjCustomCulture;
        }
        public void LoadPlans(string emp, ScheduleBox scheduleBox1, DateTime from, DateTime to, ContextMenu objEventMenu)
        {
            string sErr = "";
            

            if (sErr != "")
                MainForm.SetMsg(sErr);
        }

        private ScheduleBoxEvent InitPlanBoxEvent(ContextMenu objEventMenu, DataRow row)
        {
            
            ScheduleBoxEvent mobjEvent = new ScheduleBoxEvent();
            

            mobjEvent.ContextMenu = objEventMenu;
            return mobjEvent;
        }
        public void LoadActiveties(string emp, ScheduleBox scheduleBox1, DateTime from, DateTime to, ContextMenu objEventMenu)
        {
            
            string sErr = "";
            FormatDate();
            
            if (sErr != "")
                MainForm.SetMsg(sErr);
        }
        public void ProcessOpenAct(string emp,
            ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent, NavigationTabs navigationTabs)
        {
            Forms.frmMain eventForm = new Forms.frmMain(MainForm);

            eventForm.EmpID = emp;
            eventForm.TabsMains = navigationTabs;

            eventForm.StrAction = "A";
            DateTime minDate = demoScheduleBox.FirstDate;
            DateTime maxDate = demoScheduleBox.FirstDate;
            if (demoScheduleBox.View == ScheduleBoxView.Month)
                maxDate = demoScheduleBox.FirstDate.AddDays(31);
            else
                maxDate = demoScheduleBox.FirstDate.AddDays(8);


            if (_actMinDate != null && minDate < _actMinDate.Value)
                minDate = _actMinDate.Value;
            if (_actMaxDate != null && maxDate > _actMaxDate.Value)
                maxDate = _actMaxDate.Value;

            eventForm.SetTimeRange(minDate, maxDate);
            //eventForm.Obj = String.Format("{0}={1}&MinDate={2:yyyy-MM-dd}&MaxDate={3:yyyy-MM-dd}&Action={4}", DTO.TVCActivitiesInfo.Field.ActivityID, actInf.ActivityID, minDate, maxDate, eventForm.StrAction);
            ScheduleBoxEvent selectedEvent = objEvent;
            //List<ScheduleBoxEvent> events = new List<ScheduleBoxEvent>();
            //events.Add(selectedEvent);
            eventForm.FormClosed += (form, eventargs) =>
            {
                if (((Form)form).DialogResult == DialogResult.OK)
                {
                    string sErr = "";
                    LoadActiveties(emp, demoScheduleBox, objEvent.ContextMenu);
                }
            };
            eventForm.ShowDialog();

        }

        public void ProcessReCreatePlan(string emp, ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {
            
        }

        public void ProcessMakeCoach(string user, string emp, ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent)
        {

            
        }

        public void ProcessReCreateAct(string emp,
            ScheduleBox demoScheduleBox, ScheduleBoxEvent objEvent, NavigationTabs navigationTabs)
        {
            
        }
    }
}
